package homework;

import java.util.Arrays;
import java.util.Random;

public class Day5标准练习 {
    public static void main(String[] args) {
        System.out.println("1.输出九九乘法表:");
        for (int i =1; i <=9 ; i++) {
            for (int j = 1; j <=i ; j++) {
                System.out.print(j+"*"+i+"="+j*i+"\t");
            }
            System.out.println();
        }
        System.out.println("2. 数组小代码练习：声明、初始化、访问、遍历、复制:");
        int[] arr = new int[10];
        int[] arr2 = new int[]{1,2,3,4,5};
        int[] arr3 = {6,7,8,9,10};
        System.out.print(arr3[0]+" ");
        System.out.print(arr2[0]+" ");
        System.out.println(arr[0]+" ");
        for (int num:arr3){
            System.out.print(num+" ");
        }
        System.out.println();
        arr2=Arrays.copyOf(arr3,9);
        for (int num:arr2){
            System.out.print(num+" ");
        }
        System.out.println();
        System.arraycopy(arr3,0,arr,2,5);
        for (int num:arr){
            System.out.print(num+" ");
        }
        System.out.println();
        int[] arr4 ;
        arr4 = Arrays.copyOfRange(arr3,0,5);//这个方法指定了源数组即将复制的起始位置和结束位置，但是并不包含结束位置索引处的元素
        for (int num:arr4){
            System.out.print(num+" ");
        }
        System.out.println();
        System.out.println("3. MaxOfArray求数组元素最大值并放在最后一个元素的下一个位置:");
        int max = arr3[0];
        for (int num:arr3){
            if (num>max){
                max=num;
            }
        }
        System.out.println("max:"+max);

        arr3 = Arrays.copyOf(arr3,arr3.length+1);
        System.out.println(arr3.length);
        arr3[arr3.length-1] = max;
        for (int i = 0; i < arr3.length; i++) {
            System.out.print(arr3[i]+" ");
        }
        System.out.println();
        System.out.println("4. 对数组进行升序排列，并输出排序后的结果:");

        Random random = new Random();
        int [] arr5 = new int[5];
        for (int i = 0; i < arr5.length; i++) {
            arr5[i] = random.nextInt(100);
        }
        Arrays.sort(arr5);
        for (int num:arr5){
            System.out.print(num+" ");
        }

    }
}
